<template>
  <div>
    <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
      <div class="widget am-cf">
        <div class="widget-head am-cf">
          <div class="widget-title am-fl">特定链接生成</div>
        </div>
        <div class="widget-body  am-fr">
          <div class="am-form">
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                  <select2  v-model="query.areaTeamId" :options="areaTeams">
                      <option value="">区域</option>
                  </select2>
              </div>
            </div>
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
                <div class="am-form-group">
                    <el-input v-model="query.searchKeyword" size="small"   placeholder="请输入想要搜索的关键字，如：数学、Tips班等"></el-input>
                </div>
            </div>
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3 am-u-end">
              <div class="am-form-group">
                <button type="button" class="am-btn am-btn-default am-btn-success"
                        @click="createCode" ><span></span>生成
                </button>
              </div>
            </div>
          </div>
          <div class="am-u-sm-12 create-code">
            <div id="content" v-if="QRCode">{{QRCode}}</div>
            <div class="am-u-sm-12 " v-if="QRCode">
              <div class="am-form-group">
                <button type="button" class="am-btn am-btn-default am-btn-success"
                        @click="copyArticle" ><span></span>复制链接
                </button>
              </div>
            </div>
            <div class="code">
              <img :src="url" alt="" v-if="url">
              <div class="nocode" v-else>
                还未生成二维码
              </div>
            </div>
          </div>

        </div>
      </div>
    </div>
  </div>
</template>
<script>
  import io from '../../lib/io'
  import moment from 'moment'
  import conf from '../../lib/conf'

  export default{
    data:function(){
      return {
        query:{
          areaTeamId:window.config.areaTeams[0] && window.config.areaTeams[0].areaTeamId || '' ,
          searchKeyword:'',

        },
        url:'',
        QRCode:''

      }
    },
    components: {
    },
    mounted :function(){
      $(window).smoothScroll()
    },
    computed:{
        areaTeams: function () {
        var options = ( this.$root.config.areaTeams || [] )
            .map(function (item) {
            return {value: item.areaTeamId, text: item.name}
            })
        return options
        },
    },
    methods:{
      createCode(){
        this.QRCode = ""
        this.url = ""
        if(this.query.searchKeyword){
          this.QRCode = conf.studentBasePath + '?areaTeamId=' + this.query.areaTeamId + '&searchKeyword='+ this.query.searchKeyword
          this.url =  io.apiQrcodeEncode + '?content=' + encodeURIComponent( conf.studentBasePath + '?areaTeamId=' +this.query.areaTeamId + '&searchKeyword='+ this.query.searchKeyword)   
        }else{
          this.$alert('请输入关键字')
        }
       
      },
      copyArticle(event) {
        const range = document.createRange();
        range.selectNode(document.getElementById('content'));
        const selection = window.getSelection();
        if(selection.rangeCount > 0) selection.removeAllRanges();
        selection.addRange(range);
        document.execCommand('copy');
        this.$alert("复制成功！");
      }
 
    }
  }
</script>
<style lang="less" scoped>
.create-code{
  text-align:center;
  height:400px;
  padding-top:50px;
  .code{
    width:300px;
    height:300px;
    margin:0 auto;
    img{
      width:300px;
      height:300px;
    }
    .nocode{
      width:300px;
      height:300px;
      background:#e9ecf3;
      text-align: center;
      line-height:300px;
    }
  }
}
</style>
